package sublinear;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.xml.sax.SAXException;

import sublinear.common.DatabaseConnection;
import sublinear.common.SimpleLogger;
import sublinear.model.db.DbGraphModel;
import sublinear.model.db.OfflineWikipediaParser;

public class WikiPreprocessor {

	private static final Logger log = Logger.getLogger( WikiQuery.class.getName() );

	@SuppressWarnings("static-access")
	private static Options buildOptions() {
		Options options = new Options();

		Option inputfile = OptionBuilder.withArgName( "file" )
				.hasArg()
				.withDescription(  "MediaWiki formatted XML file to process" )
				.isRequired()
				.create( "in" );

		Option outfile = OptionBuilder.withArgName( "file" )
				.hasArg()
				.withDescription(  "output SQLite database file" )
				.isRequired()
				.create( "out" );

		Option logfile   = OptionBuilder.withArgName( "file" )
				.hasArg()
				.withDescription(  "use given file for log" )
				.create( "logfile" );

		options.addOption(inputfile);
		options.addOption(outfile);
		options.addOption(logfile);

		return options;
	}

	public static void usage(Options options) {
		// automatically generate the help statement
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp( "WikiProcessor", options);
	}

	public static void main(String[] args) {

		Options options = buildOptions();
		CommandLineParser parser = new BasicParser();
		try {
			// parse the command line arguments
			CommandLine line = parser.parse( options, args );

			if (line.hasOption("logfile")) {
				SimpleLogger.initializeLogging(Level.INFO, line.getOptionValue("logfile"));
			}

			String dbPath = line.getOptionValue("out");
			Connection conn = DatabaseConnection.getConnection(dbPath, false);

			DbGraphModel model = new DbGraphModel(conn);
			model.rebuildDatabase();
			
			String xmlfile = line.getOptionValue("in");
			OfflineWikipediaParser.doParse(model, xmlfile);
			
			model.deleteRedirectArticles();
			model.dropTables(false, true, false);

		}
		catch( ParseException exp ) {
			System.err.println( "Parsing failed.  Reason: " + exp.getMessage() );
			usage(options);
		} 
		catch (SQLException e) {
			log.log(Level.SEVERE, "SQL Exception: " + e.getMessage(), e);
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}